package com.leyou.trade.web;

import com.leyou.trade.dto.OrderRequestDTO;
import com.leyou.trade.entity.Order;
import com.leyou.trade.service.OrderService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/order")
public class OrderController {

    private final OrderService orderService;

    private OrderController(OrderService orderService) {
        this.orderService = orderService;
    }

    @PostMapping
    public ResponseEntity<Long> createOrder(
            @RequestBody OrderRequestDTO orderRequestDTO) {
        return ResponseEntity.status(HttpStatus.CREATED).body(this.orderService.createOrder(orderRequestDTO));
    }

    /**
     * 根据订单id查询订单信息
     * @param orderId
     * @return
     */
    @GetMapping("/{orderId}")
    public ResponseEntity<Order> queryOrderById(@PathVariable("orderId")Long orderId){

        return ResponseEntity.ok(this.orderService.getById(orderId));
    }

    /**
     * 根据订单id获取订单状态
     * @param orderId
     * @return
     */
    @GetMapping("/status/{id}")
    public ResponseEntity<Integer> getOrderStatus(@PathVariable("id")Long orderId){

        return ResponseEntity.ok(this.orderService.getById(orderId).getStatus());
    }
}
